<?php
/* this file will handle basket management */
// load wordpress defaults, make global variables/functions available
$this_file = str_replace( '\\', '/', __FILE__ );
$this_file = explode( 'wp-content', $this_file );
$this_file = $this_file[ 0 ];
require( $this_file . 'wp-blog-header.php' );
header( 'HTTP/1.1 200 OK' );

// theme options
$aOptions  = SofaSuppaStore::initOptions( false );
$use_stock = $aOptions[ 'use_stock' ];

// local stuff
$dothis    = !empty( $_POST[ 'dothis' ] ) ? $_POST[ 'dothis' ] : NULL; // action
$itemid    = !empty( $_POST[ 'itemid' ] ) ? intval( $_POST[ 'itemid' ] ) : NULL; // product id
$inrow     = !empty( $_POST[ 'inrow' ] ) ? intval( $_POST[ 'inrow' ] ) : NULL; // editstock row
$itemsize  = !empty( $_POST[ 'itemsize' ] ) ? stripslashes( strip_tags( urldecode( $_POST[ 'itemsize' ] ) ) ) : NULL; // product size
$cookid    = !empty( $_POST[ 'cookid' ] ) ? stripslashes( strip_tags( urldecode( $_POST[ 'cookid' ] ) ) ) : NULL; // cookie id
$itemqty   = !empty( $_POST[ 'itemqty' ] ) ? intval( $_POST[ 'itemqty' ] ) : NULL; // quantity
$itemstock = !empty( $_POST[ 'itemstock' ] ) ? intval( $_POST[ 'itemstock' ] ) : NULL; // in stock
$itemsel   = !empty( $_POST[ 'itemselmeta' ] ) ? stripslashes( strip_tags( urldecode( $_POST[ 'itemselmeta' ] ) ) ) : NULL; // selected row
$itemprice = !empty( $_POST[ 'itemprice' ] ) ? floatval( $_POST[ 'itemprice' ] ) : NULL; // price
$itemshipp = !empty( $_POST[ 'itemshipp' ] ) ? floatval( $_POST[ 'itemshipp' ] ) : NULL; // individual shipping price
$errcode   = !empty( $_POST[ 'errcode' ] ) ? stripslashes( strip_tags( urldecode( $_POST[ 'errcode' ] ) ) ) : NULL; // used for basket cleanup

if( $itemqty <= 0 ) $itemqty = 1;

if( $dothis == 'addnew' && wp_verify_nonce( $_POST[ 'sofa_suppastore_cc' ], 'chck_ref_cc' ) ) {
	// generate random cookie key
	$str_leng = 16;
	$chars_in = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	$real_len = strlen( $chars_in ) - 1;
	for( $iii = 0; $iii < $str_leng; $iii ++ ) { $random_string_final .= $chars_in[ mt_rand( 0, $real_len ) ]; }
	// check if in stock
	if( $use_stock == 'yes' ) { if( $itemqty > $itemstock ) $itemqty = $itemstock; }
	// make cookie
	$cook_str = $itemid . '_' . $random_string_final;
	setcookie( "sofasuppastore[$cook_str]", ( $itemid . '~' . $itemsize . '~' . $itemqty . '~' . $itemprice . '~' . $itemsel . '~' . $itemstock . '~' . $itemshipp ), 0, '/' );
	// redirect
	$send_me_back = get_permalink( intval( get_option( 'checkout' ) ) );
	wp_redirect( $send_me_back );
	exit;
}
if( $dothis == 'editme' && wp_verify_nonce( $_POST[ 'sofa_suppastore_cc' ], 'chck_ref_cc' ) ) {
	// check if in stock
	if( $use_stock == 'yes' ) { if( $itemqty > $itemstock ) $itemqty = $itemstock; }
	// handle cookie
	if( !isset( $_COOKIE[ "sofasuppastore[$cookid]" ] ) ) setcookie( "sofasuppastore[$cookid]", ( $itemid . '~' . $itemsize . '~' . $itemqty . '~' . $itemprice . '~' . $itemsel . '~' . $itemstock . '~' . $itemshipp ), 0, '/' );
	echo '<script language="javascript" type="text/javascript">setTimeout( function() { top.location.reload(); }, 100 );</script>';
} 
if( $dothis == 'delme' && wp_verify_nonce( $_POST[ 'sofa_suppastore_cc' ], 'chck_ref_cc' ) ) {
	// handle cookie
	if( !isset( $_COOKIE[ "sofasuppastore[$cookid]" ] ) ) setcookie( "sofasuppastore[$cookid]", "", time()-36000, '/' );
	echo '<script language="javascript" type="text/javascript">setTimeout( function() { top.location.reload(); }, 100 );</script>';
}
if( $dothis == 'cleanup' && wp_verify_nonce( $_POST[ 'sofa_suppastore_cc' ], 'chck_ref_cc' ) && $errcode ) {
	// handle meta data first
	$arr_critical = explode( '^', $errcode );
	array_pop( $arr_critical ); // remove last empty
	$arr_critical_len = count( $arr_critical ); // how many items?
	if( $arr_critical ) {
		$z = 0;
		while( $z < $arr_critical_len ) {
			$arr_temp = explode( '~', $arr_critical[ $z ] );
			$product_cookie = $arr_temp[ 2 ];
			if( !isset( $_COOKIE[ "sofasuppastore[$product_cookie]" ] ) ) setcookie( "sofasuppastore[$product_cookie]", "", time()-36000, '/' );
			$z ++;
		}
	}
	echo '<script language="javascript" type="text/javascript">setTimeout( function() { top.location.reload(); }, 100 );</script>';
}
if( $dothis == 'editstock' && wp_verify_nonce( $_POST[ 'sofa_suppastore_cc' ], 'chck_ref_cc' ) ) {
	// handle cookie
	if( !$itemstock ) $itemstock = 0;
	if( !$inrow ) $inrow = 0;
	if( $itemid ) {
		$single_stock = 'sofa_' . $inrow . '_textarea_stock';
		update_post_meta( $itemid, $single_stock, $itemstock );
	}
	echo '<script language="javascript" type="text/javascript">setTimeout( function() { top.location.reload(); }, 100 );</script>';
}